// todo: 持久化存储服务：本地存储服务类，简单的键值对存储
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:shared_preferences/shared_preferences.dart';

part 'local_storage.g.dart';

@riverpod
Future<SharedPreferences> prefs(ref) => SharedPreferences.getInstance();

/*  使用方式
  final prefs = await ref.watch(prefsProvider);
  # 存储
  prefs.setString('key', 'value');
  # 获取
  prefs.getString('key');
  # 删除
  prefs.remove('key');
*/

// 传统始化本地存储服务类
class LocalStorage {
  final SharedPreferences _prefs;

  LocalStorage(this._prefs);

  Future<bool> setString(String key, String value) =>
      _prefs.setString(key, value);
  String? getString(String key) => _prefs.getString(key);

  Future<bool> remove(String key) => _prefs.remove(key);
  Future<bool> clear() => _prefs.clear();
}

/*  使用方式
final prefs = await SharedPreferences.getInstance();
final localStorage = LocalStorage(prefs);
localStorage.setString('key', 'value');
*/